﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Rison.Web.Common;
using Rison.BusinessFacade.Helper;
using Rison.BusinessFacade.Common;
using Rison.BusinessFacade.Entity.NRBZ;
using Rison.BusinessFacade.Facade.NRBZ;
using Rison.DataAccess.Common;

namespace Rison.Web.NRBZ
{
    public partial class XWFB : XWFBPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadInformation();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            long returnValue = SaveInformation();
            
            if (returnValue>=0)
            {
                Response.Redirect("XWFB.aspx?DepartID=" + departid + "&type=2");
            }
            else
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert","<script>alert('保存错误');</script>");
        
        }      


        protected void btnNext_Click(object sender, EventArgs e)
        {
            long returnValue = SaveInformation();            
            if (returnValue >= 0)
            {
                Response.Redirect("Zfgk.aspx?DepartID=" + departid + "&type=2");
            }
            else
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('保存错误');</script>");
        }

        public override void LoadInformation()
        {
            LabTBRQ.Text ="填报日期：" + System.DateTime.Today.ToShortDateString();

            DataRow dr = RetriveNrbz();
            if (dr != null)
            {
                HiddenID.Value = dr["ID"].ToString();
                this.rblIfZwxw.SelectedValue = dr["IfZwxw"].ToString().Trim();
                string[] s = dr["Xwfbzq"].ToString().Split(',');
                for (int i = 0; i < s.Length; i++)
                {
                    if (s[i].ToLower() == "true")
                        this.cblXwfbzq.Items[i].Selected = true;
                    else
                        this.cblXwfbzq.Items[i].Selected = false;
                }

                this.rblIfTpxw.SelectedValue = dr["IfTpxw"].ToString().Trim();
                this.txtBssl.Text = dr["Bssl"].ToString().Trim();                  

            }
        }

        public override long SaveInformation()
        {
            NrbzEntity exam = null;
            NrbzFacade facade = new NrbzFacade();

            if (HiddenID.Value != "")
            {
                exam = facade.Retrieve("id", HiddenID.Value) as NrbzEntity;
                exam.ID = HiddenID.Value;
            }

            else
            {
                exam = new NrbzEntity();
                exam.ID = System.Guid.NewGuid().ToString(); ;
                exam.Year = this.year;
                exam.DepartID = this.departid.ToString();
            }
                        
            exam.IfZwxw = this.rblIfZwxw.SelectedValue.Trim();
            for (int i = 0; i < this.cblXwfbzq.Items.Count; i++)
            {
               if(i==0)
                   exam.Xwfbzq = this.cblXwfbzq.Items[i].Selected.ToString();
               else
                   exam.Xwfbzq += "," + this.cblXwfbzq.Items[i].Selected.ToString();
            }
            exam.IfTpxw = this.rblIfTpxw.SelectedValue.Trim();
            exam.Bssl = this.txtBssl.Text.Trim();

            //exam.LastModifiedTime = System.DateTime.Now.ToString();
            //exam.LastModified = userid;             

            long returnValue = 0;
            if (HiddenID.Value != "")
            {
                returnValue = facade.Update(exam);

            }
            else
            {
                //exam.CreateTime = System.DateTime.Now.ToString();
                //exam.Creator = userid;

                returnValue = facade.Save(exam);
            }

            return returnValue;
        }
    }
}
